package com.cxd.app.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONObject;
import com.cxd.base.controller.BaseController;
import com.cxd.base.util.yeepay.Dom4jUtil;
import com.cxd.message.exception.AppConstants;
import com.cxd.message.exception.CFCASignUtil;

/**
 * 易宝callBack 显示页面
 * @author wtj
 * @date 2015年12月26日 下午5:26:19
 * @company CXD
 * @version [Copyright (c) 2015 V001]
 */

@Controller("YeePayCallbackController")
@RequestMapping("yeePayCallbackc")
public class YeePayCallbackController extends BaseController{
	private static final Logger logger = Logger.getLogger(YeePayCallbackController.class);
	
	/**
	 * 实名认证 (易宝 - 开户成功)
	 * @author wtj
	 * @date 2015年12月26日下午5:28:38
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "create_account", method = {RequestMethod.GET,RequestMethod.POST })
	public void create_account(HttpServletRequest request,HttpServletResponse response)throws IOException {
		Map<String, Object> root = this.checkIntegrity(request,"恭喜您开户成功");
		this.writeTemplate(response, root, "/tpl/tip/success.html");
	}
	
	/**
	 * 充值
	 * @author wtj
	 * @date 2015年12月26日下午5:28:38
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "recharge", method = {RequestMethod.GET,RequestMethod.POST })
	public void recharge(HttpServletRequest request,HttpServletResponse response)throws IOException {
		Map<String, Object> root = this.checkIntegrity(request,"恭喜您充值成功");
		root.put("goBack", "<a href='"+AppConstants.PublicConstants.APP_RECHARGE_BACK_INTERCEPT+"'>返回</a>");
		this.writeTemplate(response, root, "/tpl/tip/success.html");
	}
	
	
	/**
	 * 提现
	 * @author wtj
	 * @date 2015年12月26日下午5:28:38
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "withdraw_cash", method = {RequestMethod.GET,RequestMethod.POST })
	public void withdraw_cash(HttpServletRequest request,HttpServletResponse response)throws IOException {
		Map<String, Object> root = this.checkIntegrity(request,"恭喜您提现成功");
		this.writeTemplate(response, root, "/tpl/tip/success.html");
	}
	
	/**
	 * 绑卡成功
	 * @author wtj
	 * @date 2016年1月11日下午12:03:51
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "binding_card", method = {RequestMethod.GET,RequestMethod.POST })
	public void binding_card(HttpServletRequest request,HttpServletResponse response)throws IOException {
		Map<String, Object> root = this.checkIntegrity(request,"恭喜您绑卡成功");
		this.writeTemplate(response, root, "/tpl/tip/success.html");
	}
	
	
	/**
	 * 投资
	 * @author wtj
	 * @date 2015年12月26日下午5:28:38
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "invest", method = {RequestMethod.GET,RequestMethod.POST })
	public void invest(HttpServletRequest request,HttpServletResponse response)throws IOException {
		Map<String, Object> root = this.checkIntegrity(request,"恭喜您投资成功");
		this.writeTemplate(response, root, "/tpl/tip/success.html");
	}
	
	
	/**
	 * 修改易宝密码
	 * @author wtj
	 * @date 2015年12月26日下午5:28:38
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "reset_password", method = {RequestMethod.GET,RequestMethod.POST })
	public void reset_password(HttpServletRequest request,HttpServletResponse response)throws IOException {
		Map<String, Object> root = this.checkIntegrity(request,"修改密码成功");
		this.writeTemplate(response, root, "/tpl/tip/success.html");
	}
	
	/**
	 * 修改手机号
	 * @author wtj
	 * @date 2015年12月26日下午5:28:38
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "reset_mobile", method = {RequestMethod.GET,RequestMethod.POST })
	public void reset_mobile(HttpServletRequest request,HttpServletResponse response)throws IOException {
		Map<String, Object> root = this.checkIntegrity(request,"修改手机号成功");
		this.writeTemplate(response, root, "/tpl/tip/success.html");
	}
	
	private Map<String, Object> checkIntegrity(HttpServletRequest request,String msg){
		Map<String, Object> root = new HashMap<String,Object>();
		// 响应的参数 为xml格式
		String respXML = request.getParameter("resp");
		// 签名
		String sign = request.getParameter("sign");
		if(AppConstants.PublicConstants.YEE_IS_TEST.equals("true")||CFCASignUtil.isVerifySign(respXML, sign)){
			Map<String, String> resultMap = Dom4jUtil.xmltoMap(respXML);
			if(resultMap.get("code")!=null){
				if(resultMap.get("code").equals("1")){
					root.put("msg", msg);
				}else{
					root.put("msg", resultMap.get("description"));
				}
			}else{
				root.put("msg", "操作失败，请联系管理员");
				logger.error("code:"+root.get("code")+"--"+JSONObject.toJSONString(resultMap));
			}
		}else{
			root.put("msg", "签名认证失败");
			logger.error("respXML:"+respXML);
			logger.error("sign:"+sign);
		}
		return root;
	}
}
